/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trackreggui.swing;

import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.InputEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JTree;
import javax.swing.TransferHandler;
import javax.swing.tree.TreePath;
import webreg.model.Athlet;
import webreg.model.Disciplin;
import webreg.model.swing.CompetitionTreeModel;

/**
 *
 * @author STKNOLL
 */
public class TreeTransferHandler extends TransferHandler {

    private JTree mTreeToHandle;
    
    public TreeTransferHandler(JTree inTree) {
        mTreeToHandle = inTree;
    }

    @Override
    public boolean canImport(TransferHandler.TransferSupport info) {
        System.out.println("GUI: TreeTransferHandler Can Import: " + info.getTransferable().getClass() );
        boolean toReturn = false;
        
        // we only import athlets and disciplins
        if ( (info.getSourceDropActions() == COPY) && (info.isDataFlavorSupported( AthletTransferable.getFlavor() )) ) {
            toReturn = true;
            System.out.println("GUI: TreeTransferHandler Import is possible");
        }
        else if ( (info.getSourceDropActions() == COPY) && (info.isDataFlavorSupported( DisciplinTransferable.getFlavor() )) ) {
            toReturn = true;
            System.out.println("GUI: TreeTransferHandler Import is possible");
        }
        return toReturn;
    }

    @Override
    public boolean importData(TransferHandler.TransferSupport info) {
        boolean toReturn = false;
        
        System.out.println("GUI: TreeTransferHandler importData()");
        if (! info.isDrop())
        {   // this is copy and paste
            return false;
        }
        else
        {   // this is a drop
            // Check for String flavor
            if ((info.getSourceDropActions() == COPY) && (info.isDataFlavorSupported(AthletTransferable.getFlavor()))) {
                System.out.println("GUI: TreeTransferHandler accepts Athlet Transferable.");
                toReturn = true;
            }
            else if ((info.getSourceDropActions() == COPY) && (info.isDataFlavorSupported(DisciplinTransferable.getFlavor()))) {
                System.out.println("GUI: TreeTransferHandler accepts Disciplin Transferable.");
                toReturn = true;
            }
            else {
                System.out.println("GUI: TreeTransferHandler does not accept this Transferable.");
                return false;
            }

            JTree.DropLocation dl = (JTree.DropLocation) info.getDropLocation();
            int index = dl.getChildIndex();
            System.out.println("GUI: TreeTransferHandler Index: " + index + "  dropLocation: " + dl.toString() );
            CompetitionTreeModel treeModel = (CompetitionTreeModel) mTreeToHandle.getModel();
            TreePath thePath = dl.getPath();

            if (thePath == null) {
                thePath = new TreePath( treeModel.getRoot() );
            }

            // create a new copy of the athlet - or adding disciplins will modify the athlet pool
            try {
                if (info.isDataFlavorSupported(AthletTransferable.getFlavor()) ) {
                    Athlet newAthlet = (Athlet) ( info.getTransferable().getTransferData( AthletTransferable.getFlavor() ) );
                    treeModel.addElementAt(thePath, (Athlet)newAthlet.clone() );
                }
                else if (info.isDataFlavorSupported(DisciplinTransferable.getFlavor()) ) {
                    Disciplin newDisciplin = (Disciplin) ( info.getTransferable().getTransferData( DisciplinTransferable.getFlavor() ) );
                    treeModel.addElementAt(thePath, newDisciplin);
                }
            } catch (UnsupportedFlavorException ex) {
                Logger.getLogger(TreeTransferHandler.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            } catch (IOException ex) {
                Logger.getLogger(TreeTransferHandler.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        }
        return toReturn;
    }

    @Override
    protected Transferable createTransferable(JComponent arg0) {
        System.out.println("GUI: TreeTransferHandler createTransferable");
        if (arg0 instanceof JTree) {
            JTree theTree = (JTree) arg0;
            if ( theTree.getSelectionPath().getLastPathComponent() instanceof Disciplin) {
                DisciplinTransferable transferable = new DisciplinTransferable( (Disciplin) theTree.getSelectionPath().getLastPathComponent());
                return transferable;
            }
            else if (theTree.getSelectionPath().getLastPathComponent() instanceof Athlet ) {
                return new AthletTransferable( (Athlet) theTree.getSelectionPath().getLastPathComponent());
            }
        }
        return null;
    }

    @Override
    protected void exportDone(JComponent arg0, Transferable arg1, int action) {
        System.out.println("Export done, remove data from tree model: " + arg0 + "  action: " +action);
        if (action != NONE) {
            CompetitionTreeModel treeModel = (CompetitionTreeModel) mTreeToHandle.getModel();
            if (arg0 instanceof JTree) {
                JTree theTree = (JTree) arg0;
                if ( theTree.getSelectionPath().getLastPathComponent() instanceof Disciplin) {
                    treeModel.removeElementAt( theTree.getSelectionPath() );
                }
                else if (theTree.getSelectionPath().getLastPathComponent() instanceof Athlet ) {
                    treeModel.removeElementAt( theTree.getSelectionPath() );
                }
                else {
                    System.out.println("Can not remove element");
                    //TODO: throw error here
                }
            }
        }
    }

    @Override
    public void exportAsDrag(JComponent arg0, InputEvent arg1, int arg2) {
        System.out.println("GUI: exportAsDrag");
        super.exportAsDrag(arg0, arg1, arg2);       // leave this call here
    }

    @Override
    public int getSourceActions(JComponent arg0) {
        //return super.getSourceActions(arg0);
        // has to be copy_or_move or the single click drag&drop will not work. move handled as copy
        return MOVE;
    }
    

}
